package package1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class TestForJVMInteractionWithOS {
	public static boolean showDebugLog = false;
	
	public static void main(String[] args) {
		
	}
	
	/**
	 * only valid when user.dir is from workspace
	 */
	private static String getJavaFilePath() throws IOException {
		String a = System.getProperty("user.dir");
		//D:\Program Files\Java\WorkSpace\JavaLearning
		String b = Thread.currentThread().getStackTrace()[1].getClassName();
		//package1.AutoPackEveryday
		b = b.replace(".", "\\");
		return a+"\\src"+"\\"+b+".java";
	}
	
	private static String getSystemEncodingCharset() {
		return System.getProperty("file.encoding");
	}
	
	private static String convertCharset(String str,String from, String to) throws IOException {
		return new String(str.getBytes(from),to); 
	}
	
	private static void writeToFile(String fileName, String str) throws IOException {
		File f = new File(fileName);
		System.err.println("writeToFilepath:"+fileName);
		BufferedWriter bw = new BufferedWriter(new FileWriter(f));
		bw.write(str);
		bw.close();
	}
	
	private static String getCurrentFolder() {
		return System.getProperty("user.dir");
	}
	public static void openCurrentFolder() throws IOException {
		String aa = new File(new File("a").getAbsolutePath()).getParent();
		aa = System.getProperty("user.dir");
		open(aa);
	}
	public static void open(String string) throws IOException {
		java.awt.Desktop.getDesktop().open(new File(string));
	}
	
	/**
	 * add '"' surround the path so CMD could recognize 
	 * @param string
	 * @return
	 */
	private static String quoteAddressForCMD(String string) {
		return "\""+string+"\"";
	}
	
	/**
	 *  remove the mark for some code //public void
	 *  need to think more possibility 
	 */
	private static String removeMark(String buf) {
		String mark = "//";
		int b = buf.indexOf(mark);
		String c = buf.substring(b+mark.length(),buf.length()).trim();
		return "\t\t"+c;
	}
	
	/**
	 * 1. open in eclipse: package1.AutoPackEveryday.java
	 * 2. open in cmd in desktop: AutoPackEveryday.java
	 * @return
	 */
	private static String getClassName() {
		String a = Thread.currentThread().getStackTrace()[1].getClassName();
		int b = a.indexOf(".");
		String c = a.substring(b+1, a.length());
		return c;
	}
	
	private static void pack(String s) throws IOException {
		pack(new File(s));
	}

	private static void pack(File f) throws IOException {
		String env;
		if(System.getProperty("user.dir").toLowerCase().contains("desktop")){
			run("jar vcf "+f.getName()+".zip "+f.getName());		
		}else{
			//...
		}
	}

	
	private static void run(String string) throws IOException {
		Process p = Runtime.getRuntime().exec(string);
		InputStream is1 = p.getInputStream();
		InputStream is2 = p.getErrorStream();
		BufferedReader br1 = new BufferedReader(new InputStreamReader(is1));
		BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));
		String b1,b2;
		StringBuffer sb1 = new StringBuffer();
		StringBuffer sb2 = new StringBuffer();
		while((b1=br1.readLine())!=null) sb1.append(b1+"\r\n");
		while((b2=br2.readLine())!=null) sb2.append(b2+"\r\n");
		br1.close();br2.close();
		b1 = ("Process inputStream message:\r\n"+sb1.toString()).trim();
		b2 = ("Process errorStream message:\r\n"+sb2.toString()).trim();
		if(!sb1.toString().equals("")&&showDebugLog) {
			System.err.println("Process inputStream message:\r\n"+sb1.toString().trim());
			writeToFile(getDesktop()+"\\InputStream.txt",b1);
		}
		if(!sb2.toString().equals("")&&showDebugLog) {
			System.err.println("Process errorStream message:\r\n"+sb2.toString().trim());
			writeToFile(getDesktop()+"\\ErrorStream.txt",b2);
		}
	}	
	private static String getDesktop() {
		return System.getenv("USERPROFILE")+"\\desktop";
	}
	
	
	private static void log(String ...arr) throws IOException {
		BufferedWriter bw = new BufferedWriter(new FileWriter("log.txt"));
		for(String a:arr) bw.write(a+"\r\n");
		bw.close();
	}
	
}
